﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Model;
using System.Drawing;

namespace WinSCM.Module.PropertyEditors
{

    [PropertyEditor(typeof(Boolean), false)]
    public class RadioButtonBooleanPropertyEditor : PropertyEditor
    {
        public RadioButtonBooleanPropertyEditor(Type objectType, IModelMemberViewItem model)
            : base(objectType, model)
        { }
        protected override object CreateControlCore()
        {

            RadioGroup result = new RadioGroup();
            result.MaximumSize = new System.Drawing.Size(100, 25);
            result.BackColor = Color.Transparent;
            result.Properties.Items.Add(new DevExpress.XtraEditors.Controls.RadioGroupItem(false, "False"));
            result.Properties.Items.Add(new DevExpress.XtraEditors.Controls.RadioGroupItem(true, "True"));
            result.EditValueChanged += new EventHandler(result_EditValueChanged);
            return result;
        }
        void result_EditValueChanged(object sender, EventArgs e)
        {
            WriteValue();
        }
        protected override void ReadValueCore()
        {
            (Control as RadioGroup).SelectedIndex = (Boolean)PropertyValue ? 1 : 0;
        }
        protected override object GetControlValueCore()
        {
            return (Control as RadioGroup).SelectedIndex == 0 ? false : true;
        }
    }
}
